package com.dgty.dg_common.util

import android.content.Context
import android.util.Log
import com.alibaba.sdk.android.oss.*
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
import com.alibaba.sdk.android.oss.common.OSSLog
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask
import com.alibaba.sdk.android.oss.model.OSSRequest
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult
import com.dgty.dg_common.bean.OssEntity

/**
 * Title: OssClientUtils$
 * Description:
 * Created DateTime: 2023/2/23$ 14:28$
 * Created by cacif$.
 */
class OssClientUtils {
    private var ossEntity: OssEntity? = null
    private var picTask: OSSAsyncTask<*>? = null
    var ossClient: OSS? = null
    val TAG = javaClass.toString()

    companion object {
        private var instance: OssClientUtils? = null
            get() {
                if (field == null) {
                    field = OssClientUtils()
                }
                return field
            }

        @JvmStatic
        fun get(): OssClientUtils = instance!!
    }

    open fun init(context: Context, ossEntity: OssEntity) {
        // 获取key并初始化
        this.ossEntity = ossEntity
        initOSSClient(context)
    }

    private fun initOSSClient(context: Context) {
        //填写自己的OSS外网域名
//        val endpoint = "http://oss-cn-hangzhou.aliyuncs.com"
//        OSSLog.enableLog()
        val endpoint = "http://oss-us-west-1.aliyuncs.com"
        //填写明文accessKeyId和accessKeySecret，加密官网有
        val credentialProvider: OSSCredentialProvider = OSSStsTokenCredentialProvider(ossEntity!!.accessKeyId, ossEntity!!.accessKeySecret, ossEntity!!.securityToken)
        val conf = ClientConfiguration()
        conf.connectionTimeout = 15 * 1000 // 连接超时，默认15秒
        conf.socketTimeout = 15 * 1000 // socket超时，默认15秒
        conf.maxConcurrentRequest = 5 // 最大并发请求数，默认5个
        conf.maxErrorRetry = 2 // 失败后最大重试次数，默认2次
        ossClient = OSSClient(context, endpoint, credentialProvider, conf)
    }

    open fun uploadPic(localPath: String, ossPath: String) {
//        val putPic = PutObjectRequest("publicduoguan", ossPath, localPath)
        Log.d(TAG, "localPath " + localPath + "  ossPath " + ossPath)
        val putPic = PutObjectRequest("mgaic", ossPath, localPath)
        putPic.crC64 = OSSRequest.CRC64Config.YES

        // 重复任务
        if (picTask != null) {
            picTask!!.cancel()
            picTask = null
        }

        picTask = ossClient?.asyncPutObject(putPic, object :
            OSSCompletedCallback<PutObjectRequest?, PutObjectResult?> {
            override fun onSuccess(request: PutObjectRequest?, result: PutObjectResult?) {
                // 上传成功
                Log.d(TAG, "UploadPicSuccess " + result?.serverCallbackReturnBody)
            }

            override fun onFailure(request: PutObjectRequest?, clientExcepion: ClientException, serviceException: ServiceException) {
                // 上传失败
                Log.e(TAG, "UploadPicFailure ")
            }
        })
    }

}